/**
 * @ClassName LC19
 * @Author ykx
 * @Date 2024/3/5 17:15
 * @Version 1.0
 */
public class LC19 {
    public static void main(String[] args) {
        LC19 lc19 = new LC19();
        int[] head = {1, 2, 3, 4, 6};
        ListNode node = ListNode.setData(head);
        System.out.println(lc19.removeNthFromEnd(node, 2));
    }

    public ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode result = new ListNode(0, head);
        ListNode fast = head;
        ListNode slow = result;
        for (int i = 0; i < n; i++) {
            fast = fast.next;
        }
        while (fast != null) {
            fast = fast.next;
            slow = slow.next;
        }
        slow.next = slow.next.next;
        return result.next;
    }
}
